/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.admin.dto.MaintainTimeSubmitDTO;
import org.springblade.modules.admin.dto.contract.ContractSearchDTO;
import org.springblade.modules.admin.dto.sugar.SugarSearchDTO;
import org.springblade.modules.admin.entity.*;
import org.springblade.modules.admin.enums.ElevatorMaintainStatusEnums;
import org.springblade.modules.admin.mapper.BuildingContractPlusMapper;
import org.springblade.modules.admin.service.*;
import org.springblade.modules.admin.vo.BuildingContractPlusVO;
import org.springblade.modules.admin.vo.ContractBuildingHoverVo;
import org.springblade.modules.admin.vo.ElevatorDetailHoverVO;
import org.springblade.modules.admin.vo.contract.*;
import org.springblade.modules.admin.vo.contract.v2.ContractInTimeVo;
import org.springblade.modules.admin.vo.contract.v2.ContractListVo;
import org.springblade.modules.admin.vo.contract.v2.ContractStatusCountVo;
import org.springblade.modules.admin.vo.sugar.SugerContractVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目合同信息 服务实现类
 *
 * @author ysq
 * @since 2022-03-01
 */
@Service
public class BuildingContractPlusServiceImpl extends BaseServiceImpl<BuildingContractPlusMapper, BuildingContractPlus> implements IBuildingContractPlusService {

	@Resource
	private IElevatorContractService elevatorContractService;

	@Resource
	private ElevatorService elevatorService;

	@Resource
	private BuildingService buildingService;

	@Resource
	private IMaintainPlanConfigService maintainPlanConfigService;

	@Resource
	private IBuildingContractPlusHistoryService buildingContractPlusHistoryService;

	@Resource
	private IBuildingContractExtService buildingContractExtService;

	@Resource
	private IBuildingContractExtPlusService buildingContractExtPlusService;

	@Resource
	private IBuildingContractMaintainTimeService buildingContractMaintainTimeService;

	@Resource
	private IMaintainContractTypeService maintainContractTypeService;

	@Resource
	@Lazy
	private IMaintainPlanContentService maintainPlanContentService;

	@Resource
	@Lazy
	private IContractLogService contractLogService;


	@Resource
	@Lazy
	private IContractAgreeService contractAgreeService;

	@Resource
	@Lazy
	private IBuildingUserService buildingUserService;

	/**
	 * 合同审核状态状态1:未审核，2:审核通过，3审核未通过
	 */
	private final Integer[] CONTRACT_CHECK_STATUS = {0, 1, 2, 3};

	/**
	 * 合同状态1:未生效2:已生效3:已到期4:已终止,
	 */
	private final Integer[] CONTRACT_STATUS = {0, 1, 2, 3, 4, 5};

	/**
	 * 合同扩展状态
	 * 1:待续签
	 * 2:已续签
	 * 3:即将到期
	 * 4:已脱保
	 * 5:已作废
	 */
	private final Integer[] CONTRACT_EXT_STATUS = {0, 1, 2, 3, 4, 5};

	@Override
	public IPage<BuildingContractPlus> selectBuildingContractPlusPage(IPage<BuildingContractPlus> page, BuildingContractPlusVO buildingContractPlus) {

		if (buildingContractPlus.getCreateBeginTime() != null && buildingContractPlus.getCreateBeginTime().equals(buildingContractPlus.getCreateEndTime())) {
			Date endTime = buildingContractPlus.getCreateEndTime();
			DateTime dateTime = DateUtil.parse(DateUtil.format(endTime, "yyyy-MM-dd") + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
			buildingContractPlus.setCreateEndTime(dateTime);
		}
		List<BuildingContractPlus> buildingContractPlusVOS;
		if (buildingContractPlus.getSearchType() != null) {
			if (buildingContractPlus.getSearchType() == 2) {
				List<Long> conIds = baseMapper.hasPlanContract().stream().map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
				buildingContractPlus.setContractIds(conIds);
			}
			buildingContractPlusVOS = baseMapper.selectBuildingContractPlusPageV2(page, buildingContractPlus);
		} else {
			buildingContractPlusVOS = baseMapper.selectBuildingContractPlusPage(page, buildingContractPlus);
		}
		for (BuildingContractPlus buildingContractPlusVO : buildingContractPlusVOS) {
			Integer elevatorCounts1 = Convert.toInt(buildingContractPlusVO.getElevatorCounts(), 0);
			List<ContractBuildingInfoVo> buildingInfo = buildingContractPlusVO.getBuildingInfo();
			if (buildingInfo == null || buildingInfo.size() == 0) {
				continue;
			}
			for (ContractBuildingInfoVo contractBuildingInfoVo : buildingInfo) {
				buildingContractPlusVO.setBuildingElevatorCounts(contractBuildingInfoVo.getElevatorCounts());
				elevatorCounts1 = elevatorCounts1 + Convert.toInt(contractBuildingInfoVo.getElevatorCounts(), 0);

			}

			buildingContractPlusVO.setElevatorCounts(elevatorCounts1);
		}
		return page.setRecords(buildingContractPlusVOS);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean addOrUpdate(BuildingContractPlus buildingContractPlus) {
		//加操作日志
		BuildingContractExtPlus buildingContractExtPlus = buildingContractPlus.getBuildingContractExtPlus();
		if (buildingContractExtPlus != null) {
			buildingContractExtPlus.setId(null);
			buildingContractPlus.setBuildingContractExtPlus(buildingContractExtPlus);
		}
		Long plusId = buildingContractPlus.getId();
		//校验合同编号是否重复
		if (StrUtil.isNotEmpty(buildingContractPlus.getContractNo())) {
			List<BuildingContractPlus> list = this.list(Wrappers.<BuildingContractPlus>lambdaQuery().eq(BuildingContractPlus::getContractNo, buildingContractPlus.getContractNo()).eq(BuildingContractPlus::getDisDeleted, false));
			if (list.size() > 0) {
				Long id = list.get(0).getId();
				if (!id.equals(buildingContractPlus.getId())) {
					throw new ServiceException("合同编号:" + buildingContractPlus.getContractNo() + "已经被占用，请修改合同编号");
				}
			}
		}
		List<ContractBuildingInfoVo> buildingInfo = buildingContractPlus.getBuildingInfo();
		if (buildingContractPlus.getId() != null) {
			BuildingContractPlus contractPlus = this.getById(buildingContractPlus.getId());
			if (buildingContractPlus.getChangeType() != null) {
				buildingContractPlus.setChangeTime(new Date());
			}
			//终止合同的逻辑判断
			if (buildingContractPlus.getStopTime() != null) {
				final String yyyyMMdd = DateUtil.format(buildingContractPlus.getStopTime(), "yyyyMMdd");
				final String yyyyMMdd1 = DateUtil.format(new Date(), "yyyyMMdd");
				if (yyyyMMdd.equals(yyyyMMdd1)) {
					buildingContractPlus.setContractStatus(CONTRACT_STATUS[4]);
				}
				if (buildingContractPlus.getStopTime().getTime() >= contractPlus.getContractEndTime().getTime() || buildingContractPlus.getStopTime().getTime() <= contractPlus.getContractBeginTime().getTime()) {
					throw new ServiceException("合同终止时间不能大于或者小于服务时间");
				}
			} else {
				//如果终止时间为空，判断数据库的终止时间是否为空,或者前端更新时每次传过来,mybatis-plus取消了空值不更新,需要重新赋值
				if (contractPlus.getStopTime() != null) {
					buildingContractPlus.setStopTime(contractPlus.getStopTime());
				}
			}
			//审核通过的逻辑判断
			if (buildingContractPlus.getStatus() != null) {
				if (buildingInfo != null && buildingInfo.size() > 0) {
					final Long elevatorId = buildingInfo.get(0).getElevatorId();
					if (elevatorId == null) {
						throw new ServiceException("电梯信息不允许为空");
					}
				}
				//表示审核通过状态
				if (buildingContractPlus.getStatus().equals(CONTRACT_CHECK_STATUS[2])) {
					buildingContractPlus.setCheckTime(new Date());
					buildingContractPlus.setAuditorName(AuthUtil.getNickName());
					List<ElevatorContract> list = elevatorContractService.list(Wrappers.<ElevatorContract>lambdaQuery().eq(ElevatorContract::getContractId, buildingContractPlus.getId()));
					saveOrUpdatePlanConfig(list);
					Date contractBeginTime = contractPlus.getContractBeginTime();
					Date contractEndTime = contractPlus.getContractEndTime();
					Date date = new Date();
					if (date.getTime() > contractBeginTime.getTime() && date.getTime() < contractEndTime.getTime()) {
						buildingContractPlus.setContractStatus(CONTRACT_STATUS[2]);
						List<Long> collect = list.stream().map(ElevatorContract::getElevatorId).collect(Collectors.toList());
						List<MaintainPlanConfig> configList = maintainPlanConfigService.list(Wrappers.<MaintainPlanConfig>lambdaQuery().in(MaintainPlanConfig::getElevatorId, collect));
						for (MaintainPlanConfig maintainPlanConfig : configList) {
							maintainPlanConfig.setMaintainStatus(ElevatorMaintainStatusEnums.NO_STATUS.getId());
						}
						maintainPlanConfigService.updateBatchById(configList);
					} else {
						buildingContractPlus.setContractStatus(CONTRACT_STATUS[1]);
					}
					if (DateUtil.betweenDay(date, contractEndTime, true) < 30L) {
						buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[3]);
					}
					if (date.getTime() > contractEndTime.getTime()) {
						buildingContractPlus.setContractStatus(CONTRACT_STATUS[3]);
						buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[1]);
					}

					//申请通过时新增历史记录
					final List<BuildingContractPlusHistory> list2 = buildingContractPlusHistoryService.list(Wrappers.<BuildingContractPlusHistory>lambdaQuery().eq(BuildingContractPlusHistory::getContractPlusId, buildingContractPlus.getId()).between(BuildingContractPlusHistory::getCreateTime, DateUtil.beginOfDay(new Date()), DateUtil.endOfDay(new Date())));
					if (list2.size() == 0) {
						BuildingContractPlusHistory buildingContractPlusHistory = new BuildingContractPlusHistory();
						BeanUtil.copyProperties(contractPlus, buildingContractPlusHistory);
						buildingContractPlusHistory.setId(null);
						buildingContractPlusHistory.setContractPlusId(contractPlus.getId());
						buildingContractPlusHistoryService.save(buildingContractPlusHistory);
					}
					//审核通过更新电梯的状态
					for (ElevatorContract elevatorContract : list) {
						updateElevator(elevatorContract.getElevatorId(), Convert.toInt(contractPlus.getContractSource(), -5), 1);
					}
				} else if (buildingContractPlus.getStatus().equals(CONTRACT_CHECK_STATUS[3])) {
					buildingContractPlus.setContractStatus(-1);
					buildingContractPlus.setContractExtStatus(-1);
					buildingContractPlus.setCheckTime(new Date());
				}

			}

			//脱保
			if (Convert.toInt(buildingContractPlus.getContractExtStatus(), -1).equals(CONTRACT_EXT_STATUS[4])) {
				buildingContractPlus.setTuoDate(new Date());
				List<BuildingContractPlusHistory> list = buildingContractPlusHistoryService.list(Wrappers.<BuildingContractPlusHistory>lambdaQuery().eq(BuildingContractPlusHistory::getContractPlusId, buildingContractPlus.getId()));
				if (list.size() > 0) {
					BuildingContractPlusHistory buildingContractPlusHistory1 = list.get(0);
					if (!buildingContractPlusHistory1.getContractExtStatus().equals(CONTRACT_EXT_STATUS[4])) {
						buildingContractPlusHistory1.setContractExtStatus(buildingContractPlus.getContractExtStatus());
						buildingContractPlusHistoryService.updateById(buildingContractPlusHistory1);
					}
				}
			}
		} else if (buildingInfo == null || buildingInfo.size() == 0) {
			throw new ServiceException("项目信息不允许为空");
		}

		List<ContractBuildingInfoVo> list = new ArrayList<>(16);
		List<ElevatorContract> elevatorContracts = new ArrayList<>(16);

		if (buildingInfo != null) {
			elevatorContractService.remove(Wrappers.<ElevatorContract>lambdaQuery().eq(ElevatorContract::getContractId, buildingContractPlus.getId()));
			List<Long> buildingIds = buildingInfo.stream().map(ContractBuildingInfoVo::getBuildingId).distinct().collect(Collectors.toList());
			Map<Long, List<ContractBuildingInfoVo>> map = buildingInfo.stream().collect(Collectors.groupingBy(ContractBuildingInfoVo::getBuildingId));
			for (Long buildingId : buildingIds) {
				List<ContractBuildingInfoVo> contractBuildingInfoVos = map.get(Convert.toLong(buildingId));
				ContractBuildingInfoVo contractBuildingInfoVo1 = contractBuildingInfoVos.get(0);
				for (ContractBuildingInfoVo buildingInfoVo : contractBuildingInfoVos) {
					if (isExistContractByDates(buildingInfoVo.getElevatorId(), buildingContractPlus.getContractBeginTime(), buildingContractPlus.getContractEndTime(), buildingContractPlus.getId(), buildingContractPlus.getContractType())) {
						throw new ServiceException("该合同所选电梯,已经生成了其它合同");
					}
					ElevatorContract elevatorContract = new ElevatorContract();
					BeanUtil.copyProperties(buildingInfoVo, elevatorContract);
					elevatorContract.setElevatorId(buildingInfoVo.getElevatorId());
					elevatorContract.setContractType(buildingContractPlus.getContractType());
					elevatorContract.setContractBeginTime(buildingContractPlus.getContractBeginTime());
					elevatorContract.setContractEndTime(buildingContractPlus.getContractEndTime());
					elevatorContract.setServeCounts(buildingInfoVo.getServeCounts());
					MaintainContractType contractType = maintainContractTypeService.getOneNotDelete(Convert.toLong(buildingInfoVo.getMaintainType()));
					contractType.setExtUrls(buildingInfoVo.getExtUrls());
					elevatorContract.setContractTypeHistoryInfo(contractType);
					elevatorContracts.add(elevatorContract);
					List<ElevatorContract> list2 = elevatorContractService.getList(buildingInfoVo.getElevatorId());
					if (list2.size() == 0) {
						updateElevator(buildingInfoVo.getElevatorId(), Convert.toInt(buildingContractPlus.getContractSource(), -5), null);
					} else {
						updateElevator(buildingInfoVo.getElevatorId(), Convert.toInt(this.getById(list2.get(0).getContractId()).getContractSource(), -5), null);
					}
				}
				contractBuildingInfoVo1.setElevatorCounts(contractBuildingInfoVos.size());
			}
		}
		//添加日志
		BuildingContractPlus oldBuildingContractPlus = null;
		if (plusId != null) {
			oldBuildingContractPlus = this.getById(plusId);
		}
		boolean save = this.saveOrUpdate(buildingContractPlus);
		if (elevatorContracts.size() > 0) {
			elevatorContracts.forEach(e -> e.setContractId(buildingContractPlus.getId()));
			elevatorContractService.saveBatch(elevatorContracts);
		}

		//如果父id不为空表示是续签功能
		if (buildingContractPlus.getParentId() != null) {
			BuildingContractPlus contractPlus = this.getById(buildingContractPlus.getParentId());
			BuildingContractPlus oldContractPlus = new BuildingContractPlus();
			BeanUtil.copyProperties(contractPlus, oldContractPlus);
			contractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[2]);
			this.updateById(contractPlus);
			contractLogService.saveLog(oldContractPlus, contractPlus);

			//更新为已回签
			ContractAgree contractAgree = new ContractAgree();
			contractAgree.setId(buildingContractPlus.getAgreeId());
			contractAgree.setContractId(contractPlus.getId());
			//6表示将协议更新已续签
			contractAgree.setAgreeStatus(6);
			contractAgreeService.updateStatus(contractAgree);
		}

		//id为空添加附件的历史记录
		if (plusId == null) {
			List<BuildingContractExt> list2 = new ArrayList<>(16);
			String userName = AuthUtil.getUserName();
			final List<ContractExtVo> contractExt = buildingContractPlus.getContractExt();
			if (contractExt != null && contractExt.size() > 0) {
				for (ContractExtVo contractExtVo : contractExt) {
					BuildingContractExt buildingContractExt = new BuildingContractExt();
					buildingContractExt.setContractId(buildingContractPlus.getId());
					buildingContractExt.setUrl(contractExtVo.getUrl());
					buildingContractExt.setName(contractExtVo.getName());
					buildingContractExt.setCreateUserName(userName);
					buildingContractExt.setAddType(1);
					list2.add(buildingContractExt);
				}
				if (list.size() > 0) {
					buildingContractExtService.saveBatch(list2);
				}
			}

			//2024-03-11新增v2需求

		}
		//2022-11-23 需求调整为新增初始化config配置记录
		List<ElevatorContract> list3 = elevatorContractService.list(Wrappers.<ElevatorContract>lambdaQuery().eq(ElevatorContract::getContractId, buildingContractPlus.getId()));
		if (list3.size() > 0) {
			saveOrUpdatePlanConfig(list3);
		}

		//2024-03-11 迭代升级v2-新增服务内容
		saveContractExtV2(buildingContractPlus);
		contractLogService.saveLog(oldBuildingContractPlus, buildingContractPlus);
		return save;
	}

	@Override
	public ContractStatisticVo getContractStatisticInfo() {
		ContractStatisticVo contractStatisticInfo1 = baseMapper.getContractStatisticInfo();
		ContractStatisticVo contractStatisticInfo2 = baseMapper.getContractStatisticElevatorInfo();
		contractStatisticInfo1.setContractElevatorCount(contractStatisticInfo2.getContractElevatorCount());
		contractStatisticInfo1.setDisContractElevatorCount(contractStatisticInfo2.getDisContractElevatorCount());
		contractStatisticInfo1.setEnContractElevatorCount(contractStatisticInfo2.getEnContractElevatorCount());
		contractStatisticInfo1.setPastContractElevatorCount(contractStatisticInfo2.getPastContractElevatorCount());
		contractStatisticInfo1.setCreateContractElevatorCount(contractStatisticInfo2.getCreateContractElevatorCount());
		contractStatisticInfo1.setNewElevatorContractElevatorCount(contractStatisticInfo2.getNewElevatorContractElevatorCount());
		contractStatisticInfo1.setDisExtensionContractElevatorCount(contractStatisticInfo2.getDisExtensionContractElevatorCount());
		contractStatisticInfo1.setExtensionContractElevatorCount(contractStatisticInfo2.getExtensionContractElevatorCount());
		contractStatisticInfo1.setAboutContractElevatorCount(contractStatisticInfo2.getAboutContractElevatorCount());
		contractStatisticInfo1.setStopContractElevatorCount(contractStatisticInfo2.getStopContractElevatorCount());
		contractStatisticInfo1.setOverdueContractElevatorCount(contractStatisticInfo2.getOverdueContractElevatorCount());
		contractStatisticInfo1.setOutContractElevatorCount(contractStatisticInfo2.getOutContractElevatorCount());

		return contractStatisticInfo1;
	}


	@Override
	public BuildingContractPlus getDetailByElevator(Long elevatorId) {
		return baseMapper.getDetailByElevator(elevatorId);
	}

	@Override
	public Boolean isExistContractByDates(Long elevatorId, Date beginDate, Date endDate, Long contractId, int contractType) {

		List<ElevatorContract> elevatorContracts = elevatorContractService.getList(elevatorId);
		if (elevatorContracts.size() == 0) {
			return false;
		}
		elevatorContracts = elevatorContracts.stream().filter(o -> !o.getContractId().equals(contractId)).collect(Collectors.toList());
		for (ElevatorContract elevatorContract : elevatorContracts) {

			Date contractBeginTime = elevatorContract.getContractBeginTime();
			Date contractEndTime = elevatorContract.getContractEndTime();
			String dateDesc = "(" + DateUtil.format(contractBeginTime, "yyyy/MM/dd") + "-" + DateUtil.format(contractEndTime, "yyyy/MM/dd") + ")";
			final BuildingContractPlus buildingContractPlus = this.getById(elevatorContract.getContractId());
			if (buildingContractPlus.getStopTime() != null) {
				contractEndTime = buildingContractPlus.getStopTime();
			}
			final Integer contractStatus = Convert.toInt(buildingContractPlus.getContractStatus(), -1);
			final Integer contractExtStatus = Convert.toInt(buildingContractPlus.getContractExtStatus(), -1);
			if (beginDate.getTime() >= contractBeginTime.getTime() && beginDate.getTime() <= contractEndTime.getTime() && contractType == elevatorContract.getContractType() && !contractStatus.equals(CONTRACT_STATUS[4]) && !contractExtStatus.equals(CONTRACT_EXT_STATUS[5])) {
				final Building building = buildingService.getById(elevatorContract.getBuildingId());
				final BuildingContractPlus contractPlus = this.getById(elevatorContract.getContractId());
				if (beginDate.getTime() >= contractBeginTime.getTime() && beginDate.getTime() <= contractEndTime.getTime()) {
					throw new ServiceException("【" + building.getName() + "】" + "【" + BuildingContractPlusEnums.getcontractTypeName(elevatorContract.getContractType()) + "】" + "日期已经被" + "【" + contractPlus.getContractNo() + "】" + dateDesc + "合同占用,请修改合同时间");
				} else {
					throw new ServiceException("【" + building.getName() + "】" + "【" + BuildingContractPlusEnums.getcontractTypeName(elevatorContract.getContractType()) + "】" + "日期已经被" + "【" + contractPlus.getContractNo() + "】" + dateDesc + "合同占用,请修改合同类型");
				}
			}
			if (endDate.getTime() >= contractBeginTime.getTime() && beginDate.getTime() <= contractEndTime.getTime() && contractType == elevatorContract.getContractType() && !contractStatus.equals(CONTRACT_STATUS[4]) && !contractExtStatus.equals(CONTRACT_EXT_STATUS[5])) {
				final Building building = buildingService.getById(elevatorContract.getBuildingId());
				final BuildingContractPlus contractPlus = this.getById(elevatorContract.getContractId());
				if (beginDate.getTime() >= contractBeginTime.getTime() && beginDate.getTime() <= contractEndTime.getTime()) {
					throw new ServiceException("【" + building.getName() + "】" + "【" + BuildingContractPlusEnums.getcontractTypeName(elevatorContract.getContractType()) + "】" + "日期已经被" + "【" + contractPlus.getContractNo() + "】" + dateDesc + "合同占用,请修改合同时间");
				} else {
					throw new ServiceException("【" + building.getName() + "】" + "【" + BuildingContractPlusEnums.getcontractTypeName(elevatorContract.getContractType()) + "】" + "日期已经被" + "【" + contractPlus.getContractNo() + "】" + dateDesc + "合同占用,请修改合同类型");
				}
			}
		}
		return false;
	}

	@Override
	public void syncData(String body) {

		Map map = JSON.parseObject(body, Map.class);
		String data = Convert.toStr(map.get("data"));
		List<Map> maps = JSON.parseArray(data, Map.class);
		List<BuildingContractPlus> list = new ArrayList<>(50);
		for (Map value : maps) {
			BuildingContractPlus buildingContractPlus = new BuildingContractPlus();
			buildingContractPlus.setContractType(1);
			Map map1 = value;
			Integer auditState = Convert.toInt(map1.get("auditState"));
			if (auditState == 1) {
				buildingContractPlus.setStatus(CONTRACT_CHECK_STATUS[2]);
			} else if (auditState == 0) {
				buildingContractPlus.setStatus(CONTRACT_CHECK_STATUS[3]);
			}
			String auditor = Convert.toStr(map1.get("auditor"), "");
			buildingContractPlus.setAuditorName(auditor);
			Long beginDate = Convert.toLong(map1.get("beginDate"));
			buildingContractPlus.setContractBeginTime(new Date(beginDate));

			Long endDate = Convert.toLong(map1.get("endDate"));
			buildingContractPlus.setContractEndTime(new Date(endDate));

			String checkTel = Convert.toStr(map1.get("checkTel"), "");
			buildingContractPlus.setCustomerPhone(checkTel);

			String contractNo = Convert.toStr(map1.get("contractNo"), "");
			buildingContractPlus.setContractNo(contractNo);

			String customerName = Convert.toStr(map1.get("customerName"), "");
			buildingContractPlus.setCustomerUnit(customerName);

			String garrison = Convert.toStr(map1.get("garrison"), "");
			if (StrUtil.isNotEmpty(garrison) && "是".equals(garrison)) {
				buildingContractPlus.setIsLive(1);
			} else {
				buildingContractPlus.setIsLive(0);
			}

			String governorMoney = Convert.toStr(map1.get("governorMoney"), "");
			buildingContractPlus.setYearSpeedGovernorCost(governorMoney);


			String governorPayName = Convert.toStr(map1.get("governorPayName"), "");
			if (StrUtil.isNotEmpty(governorPayName) & "自缴".equals(governorPayName)) {
				buildingContractPlus.setYearSpeedGovernorCostType(3);
			}
			String maintainType = Convert.toStr(map1.get("maintainType"), "");
			if (StrUtil.isNotEmpty(maintainType) & "全包".equals(maintainType)) {
				buildingContractPlus.setMaintainType(3);
			} else if (StrUtil.isNotEmpty(maintainType) & "清包".equals(maintainType)) {
				buildingContractPlus.setMaintainType(1);
			} else if (StrUtil.isNotEmpty(maintainType) & "半包".equals(maintainType)) {
				buildingContractPlus.setMaintainType(2);
			} else if (StrUtil.isNotEmpty(maintainType) & "三包".equals(maintainType)) {
				buildingContractPlus.setMaintainType(3);
			} else if (StrUtil.isNotEmpty(maintainType) & "小包".equals(maintainType)) {
				buildingContractPlus.setMaintainType(4);
			} else if (StrUtil.isNotEmpty(maintainType) & "免包".equals(maintainType)) {
				buildingContractPlus.setMaintainType(5);
			} else if (StrUtil.isNotEmpty(maintainType) & "维修".equals(maintainType)) {
				buildingContractPlus.setMaintainType(6);
			}

			String maintainUnit = Convert.toStr(map1.get("maintainUnit"), "");
			buildingContractPlus.setMaintainUnit(maintainUnit);

			String monthMoney = Convert.toStr(map1.get("monthMoney"), "");
			buildingContractPlus.setMonthCostDesc(monthMoney);

			String payCycleName = Convert.toStr(map1.get("payCycleName"), "");
			if (StrUtil.isNotEmpty(payCycleName) & "一次性付款".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(9);
			} else if (StrUtil.isNotEmpty(payCycleName) & "月结".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(1);
			} else if (StrUtil.isNotEmpty(payCycleName) & "两月结".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(2);
			} else if (StrUtil.isNotEmpty(payCycleName) & "季度结".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(3);
			} else if (StrUtil.isNotEmpty(payCycleName) & "四月结".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(4);
			} else if (StrUtil.isNotEmpty(payCycleName) & "半年结".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(5);
			} else if (StrUtil.isNotEmpty(payCycleName) & "年度结".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(6);
			} else if (StrUtil.isNotEmpty(payCycleName) & "50%,50%".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(7);
			} else if (StrUtil.isNotEmpty(payCycleName) & "预付50%,完结50%".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(8);
			} else if (StrUtil.isNotEmpty(payCycleName) & "免费".equals(payCycleName)) {
				buildingContractPlus.setPayDateDesc(10);
			}


			//1:2:	3:	4:	5:	6:	7:	8:	9:一次性付款	10:


			String payCyclePoint = Convert.toStr(map1.get("payCyclePoint"), "");
			if (StrUtil.isNotEmpty(payCyclePoint) & "周期结束".equals(payCyclePoint)) {
				buildingContractPlus.setPayDateType(1);
			} else if (StrUtil.isNotEmpty(payCyclePoint) & "周期开始".equals(payCyclePoint)) {
				buildingContractPlus.setPayDateType(3);
			} else if (StrUtil.isNotEmpty(payCyclePoint) & "周期中期".equals(payCyclePoint)) {
				buildingContractPlus.setPayDateType(2);
			}


			String receiptType = Convert.toStr(map1.get("receiptType"), "");
			if (StrUtil.isNotEmpty(receiptType) & "增值税专用发票".equals(receiptType)) {
				buildingContractPlus.setInvoiceType(1);
			} else if (StrUtil.isNotEmpty(receiptType) & "增值税普通发票".equals(receiptType)) {
				buildingContractPlus.setInvoiceType(2);
			} else if (StrUtil.isNotEmpty(receiptType) & "收据".equals(receiptType)) {
				buildingContractPlus.setInvoiceType(3);
			} else if (StrUtil.isNotEmpty(receiptType) & "已收款未开票".equals(receiptType)) {
				buildingContractPlus.setInvoiceType(4);
			} else if (StrUtil.isNotEmpty(receiptType) & "已开收据为付款".equals(receiptType)) {
				buildingContractPlus.setInvoiceType(5);
			} else if (StrUtil.isNotEmpty(receiptType) & "已开收据未开发票".equals(receiptType)) {
				buildingContractPlus.setInvoiceType(6);
			} else if (StrUtil.isNotEmpty(receiptType) & "预支发票".equals(receiptType)) {
				buildingContractPlus.setInvoiceType(7);
			} else if (StrUtil.isNotEmpty(receiptType) & "其它".equals(receiptType)) {
				buildingContractPlus.setInvoiceType(8);
			}
			String srcType = Convert.toStr(map1.get("srcType"), "");
			if (StrUtil.isNotEmpty(srcType) & "续签".equals(srcType)) {
				buildingContractPlus.setContractSource(2);
			} else if (StrUtil.isNotEmpty(srcType) & "新签".equals(srcType)) {
				buildingContractPlus.setContractSource(1);
			} else if (StrUtil.isNotEmpty(srcType) & "转签".equals(srcType)) {
				buildingContractPlus.setContractSource(3);
			} else if (StrUtil.isNotEmpty(srcType) & "丢失".equals(srcType)) {
				buildingContractPlus.setContractSource(4);
			} else if (StrUtil.isNotEmpty(srcType) & "夺回".equals(srcType)) {
				buildingContractPlus.setContractSource(5);
			} else if (StrUtil.isNotEmpty(srcType) & "新梯移交".equals(srcType)) {
				buildingContractPlus.setContractSource(6);
			} else if (StrUtil.isNotEmpty(srcType) & "免保转签".equals(srcType)) {
				buildingContractPlus.setContractSource(7);
			}

			String sumMoney = Convert.toStr(map1.get("sumMoney"), "");
			buildingContractPlus.setContractCost(sumMoney);

			String tel = Convert.toStr(map1.get(" tel"), "");
			buildingContractPlus.setCustomerPhone(tel);

			String yearCheckMoney = Convert.toStr(map1.get("yearCheckMoney"), "");
			buildingContractPlus.setYearCostDesc(yearCheckMoney);

			String yearCheckPay = Convert.toStr(map1.get("yearCheckPayName"), "");
			if (StrUtil.isNotEmpty(yearCheckPay) & "自缴".equals(yearCheckPay)) {
				buildingContractPlus.setYearCostType(3);
			}
			String id = Convert.toStr(map1.get("id"));
			Long buildingId = baseMapper.getbuildingIdByContractId(id);
			if (buildingId == null) {
				continue;
			}
			List<ElevatorDetail> elevatorDetails = elevatorService.getListByBuildingId(buildingId, null);
			if (elevatorDetails.size() == 0) {
				continue;
			}
			List<ContractBuildingInfoVo> contractBuildingInfoVos = new ArrayList<>(16);
			for (ElevatorDetail elevatorDetail : elevatorDetails) {
				ContractBuildingInfoVo contractBuildingInfoVo = new ContractBuildingInfoVo();
				contractBuildingInfoVo.setBuildingId(buildingId);
				contractBuildingInfoVo.setBuildingName(buildingService.getById(buildingId).getName());
				Long id1 = elevatorDetail.getElevatorId();
				contractBuildingInfoVo.setElevatorId(id1);
				contractBuildingInfoVos.add(contractBuildingInfoVo);
			}
			buildingContractPlus.setBuildingInfo(contractBuildingInfoVos);
			list.add(buildingContractPlus);
		}
		for (BuildingContractPlus buildingContractPlus : list) {
			try {
				addOrUpdate(buildingContractPlus);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	@Override
	public void init() {
		List<BuildingContractPlus> list = this.list();
		for (int i = 0; i < list.size(); i++) {
			BuildingContractPlus buildingContractPlus = list.get(i);
			BuildingContractPlus buildingContractPlus2 = new BuildingContractPlus();
			Long customerUnitId = buildingContractPlus.getCustomerUnitId();
			if (customerUnitId != null) {
				BuildingUser user = buildingUserService.getById(customerUnitId);
				if (user != null && user.getName() != null) {
					BuildingUser serviceOne = buildingUserService.getOne(Wrappers.<BuildingUser>lambdaQuery().eq(BuildingUser::getName, user.getName()).eq(BuildingUser::getBuildingUserType, 1));
					if (serviceOne != null) {
						buildingContractPlus2.setTenantId(buildingContractPlus.getTenantId());
						buildingContractPlus2.setIsDeleted(buildingContractPlus.getIsDeleted());
						buildingContractPlus2.setCustomerUnitId(serviceOne.getId());
						buildingContractPlus2.setCustomerUnit(serviceOne.getName());
						this.updateById(buildingContractPlus2);
					}
				}
			}
		}
	}

	@Override
	public List<BuildingContractPlus> downExcel(BuildingContractPlusVO buildingContractPlus) {

		return baseMapper.selectBuildingContractPlusPage(null, buildingContractPlus);
	}

	@Override
	public boolean cancelContract(Long id, Integer status) {
		//status 1:表示取消脱保，2：终止后开启，3：取消作废
		Integer[] STATUS_TYPE = {0, 1, 2, 3};
		BuildingContractPlus buildingContractPlus = this.getById(id);
		BuildingContractPlus oldBuildingContractPlus = JSON.parseObject(JSON.toJSONString(buildingContractPlus), BuildingContractPlus.class);
		Date date = new Date();
		Date contractEndTime = buildingContractPlus.getContractEndTime();
		Date contractBeginTime = buildingContractPlus.getContractBeginTime();
		//取消脱保
		final BuildingContractPlus contractPlus = this.getOne(Wrappers.<BuildingContractPlus>lambdaQuery().eq(BuildingContractPlus::getParentId, buildingContractPlus.getId()));
		if (status.equals(STATUS_TYPE[1])) {
			buildingContractPlus.setTuoDate(new Date(0));
			if (contractPlus != null) {
				//如果存在续签记录就更新为续签状态
				buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[2]);
			} else if (date.getTime() > contractEndTime.getTime()) {
				if (contractPlus == null) {
					buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[1]);
				} else {
					//如果当前时间大于合同的结束时间,不能取消脱保
					throw new ServiceException("合同已过期不能取消脱保状态");
				}
			} else if (DateUtil.betweenDay(date, contractEndTime, true) < 30L) {
				buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[3]);
			} else {
				buildingContractPlus.setContractExtStatus(-1);
			}

			BuildingContractPlusHistory buildingContractPlusHistory = buildingContractPlusHistoryService.getOne(Wrappers.<BuildingContractPlusHistory>lambdaQuery().eq(BuildingContractPlusHistory::getContractPlusId, id));
			if (buildingContractPlusHistory != null) {
				String yyyyMMdd1 = Convert.toStr(DateUtil.format(new Date(), "yyyyMMdd"));
				String yyyyMMdd2 = Convert.toStr(DateUtil.format(Convert.toDate(buildingContractPlusHistory.getUpdateTime(), new Date()), "yyyyMMdd"));
				if (yyyyMMdd1.equals(yyyyMMdd2)) {
					buildingContractPlusHistory.setContractExtStatus(CONTRACT_EXT_STATUS[1]);
					buildingContractPlusHistoryService.updateById(buildingContractPlusHistory);
				}
			}
			contractLogService.saveLogHistory(305, oldBuildingContractPlus, buildingContractPlus);
		} else if (status.equals(STATUS_TYPE[2])) {
			if (date.getTime() > contractEndTime.getTime()) {
				buildingContractPlus.setContractStatus(CONTRACT_STATUS[3]);
				if (contractPlus == null) {
					buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[1]);
				} else {
					buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[2]);
				}
			} else {
				if (date.getTime() > contractBeginTime.getTime() && date.getTime() < contractEndTime.getTime()) {
					buildingContractPlus.setContractStatus(CONTRACT_STATUS[2]);
					if (DateUtil.betweenDay(date, contractEndTime, true) < 30L) {
						buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[3]);
					} else {
						if (contractPlus != null) {
							buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[2]);
						} else {
							buildingContractPlus.setContractExtStatus(-1);
						}

					}
				}
				buildingContractPlus.setContractStatus(CONTRACT_STATUS[2]);
			}

			contractLogService.saveLogHistory(307, oldBuildingContractPlus, buildingContractPlus);

		} else if (status.equals(STATUS_TYPE[3])) {
			List<ElevatorContract> elevatorContracts = elevatorContractService.list(Wrappers.<ElevatorContract>lambdaQuery().eq(ElevatorContract::getContractId, id));
			for (int i = 0; i < elevatorContracts.size(); i++) {
				Long elevatorId = elevatorContracts.get(i).getElevatorId();
				if (this.isExistContractByDates(elevatorId, buildingContractPlus.getContractBeginTime(), buildingContractPlus.getContractEndTime(), id, buildingContractPlus.getContractType())) {
					throw new ServiceException("该合同所选电梯,已经生成了其它合同");
				}
			}
			if (date.getTime() > contractBeginTime.getTime() && date.getTime() < contractEndTime.getTime()) {
				buildingContractPlus.setContractStatus(CONTRACT_STATUS[2]);
				if (DateUtil.betweenDay(date, contractEndTime, true) < 30L) {
					buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[3]);
				} else {
					buildingContractPlus.setContractExtStatus(-1);
				}
			} else if (date.getTime() > contractEndTime.getTime()) {
				buildingContractPlus.setContractStatus(CONTRACT_STATUS[3]);
				if (contractPlus == null) {
					buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[1]);
				} else {
					buildingContractPlus.setContractExtStatus(CONTRACT_EXT_STATUS[2]);
				}
			} else {
				buildingContractPlus.setContractStatus(CONTRACT_STATUS[1]);
				buildingContractPlus.setContractExtStatus(-1);
			}
			contractLogService.saveLogHistory(308, oldBuildingContractPlus, buildingContractPlus);
		}
		this.updateById(buildingContractPlus);
		return true;
	}

	@Override
	public List<SugerContractVo> getSugerContractDetail() {
		Date endTime = new Date();
		DateTime startTime = DateUtil.beginOfMonth(DateUtil.offsetMonth(endTime, -5));
		List<BuildingContractPlusHistory> contractPlusList = buildingContractPlusHistoryService.list(Wrappers.<BuildingContractPlusHistory>lambdaQuery().between(BuildingContractPlusHistory::getCreateTime, startTime, endTime));
		contractPlusList.forEach(o -> o.setCreateDate(Convert.toInt(DateUtil.format(o.getCreateTime(), "MM"))));
		final List<Integer> dateList = contractPlusList.stream().map(BuildingContractPlusHistory::getCreateDate).distinct().collect(Collectors.toList());
		final Map<Integer, List<BuildingContractPlusHistory>> map = contractPlusList.stream().collect(Collectors.groupingBy(BuildingContractPlusHistory::getCreateDate));

		List<SugerContractVo> resultList = new ArrayList<>(16);
		for (Integer date : dateList) {
			SugerContractVo sugerContractVo = new SugerContractVo();
			sugerContractVo.setMonth(date);
			List<BuildingContractPlusHistory> buildingContractPluses = map.get(date);
			if (buildingContractPluses != null && buildingContractPluses.size() > 0) {
				sugerContractVo.setCreateContractElevatorCount(Convert.toInt(buildingContractPluses.stream().filter(o -> o.getContractSource() == 1).collect(Collectors.toList()).size(), 0));
				sugerContractVo.setExtensionContractCount(Convert.toInt(buildingContractPluses.stream().filter(o -> o.getContractSource() == 2).collect(Collectors.toList()).size(), 0));
				sugerContractVo.setNewElevatorContractCount(Convert.toInt(buildingContractPluses.stream().filter(o -> o.getContractSource() == 6).collect(Collectors.toList()).size(), 0));
				sugerContractVo.setFreeElevatorContractCount(Convert.toInt(buildingContractPluses.stream().filter(o -> o.getContractSource() == 7).collect(Collectors.toList()).size(), 0));
				sugerContractVo.setTransferContractCount(Convert.toInt(buildingContractPluses.stream().filter(o -> o.getContractSource() == 3).collect(Collectors.toList()).size(), 0));

			}
			resultList.add(sugerContractVo);
		}
		return resultList;
	}

	@Override
	public Integer getValidElevator(SugarSearchDTO sugarSearchDTO) {
		return baseMapper.getValidElevator(sugarSearchDTO);
	}

	@Override
	public Integer update(BuildingContractPlus buildingContractPlus) {
		return baseMapper.update(buildingContractPlus);
	}

	@Override
	public List<ContractElevatorVo> getContractElevator() {
		return baseMapper.getContractElevator();
	}

	private void saveOrUpdatePlanConfig(List<ElevatorContract> elevatorContracts) {
		List<Long> elevatorIds = elevatorContracts.stream().map(ElevatorContract::getElevatorId).collect(Collectors.toList());
		Map<Long, List<ElevatorContract>> listMap = elevatorContracts.stream().collect(Collectors.groupingBy(ElevatorContract::getElevatorId));
		List<MaintainPlanConfig> planConfigList = maintainPlanConfigService.list(Wrappers.<MaintainPlanConfig>lambdaQuery().in(MaintainPlanConfig::getElevatorId, elevatorIds));
		Map<Long, List<MaintainPlanConfig>> listMap1 = planConfigList.stream().collect(Collectors.groupingBy(MaintainPlanConfig::getElevatorId));
		List<MaintainPlanConfig> list = new ArrayList<>(16);
		for (Long elevatorId : elevatorIds) {
			ElevatorContract elevatorContract = listMap.get(elevatorId).get(0);
			List<MaintainPlanConfig> maintainPlanConfigs = listMap1.get(elevatorId);
			if (maintainPlanConfigs != null && maintainPlanConfigs.size() > 0) {
				MaintainPlanConfig maintainPlanConfig = maintainPlanConfigs.get(0);
				if (maintainPlanConfig.getFirstTime() == null) {
					maintainPlanConfig.setStartTime(elevatorContract.getContractBeginTime());
					maintainPlanConfig.setEndTime(elevatorContract.getContractEndTime());
					list.add(maintainPlanConfig);
				}
			} else {
				MaintainPlanConfig maintainPlanConfig = new MaintainPlanConfig();
				maintainPlanConfig.setBuildingId(elevatorContract.getBuildingId());
				maintainPlanConfig.setElevatorId(elevatorContract.getElevatorId());
				maintainPlanConfig.setStartTime(elevatorContract.getContractBeginTime());
				maintainPlanConfig.setEndTime(elevatorContract.getContractEndTime());
				maintainPlanConfig.setMaintainStatus(-1);
				list.add(maintainPlanConfig);
			}
		}
		maintainPlanConfigService.saveOrUpdateBatch(list);
	}

	@Override
	public List<ContractBuildingHoverVo> getHoverList(Long contractId) {
		List<ContractBuildingHoverVo> hoverList = baseMapper.getHoverList(contractId);
		// hoverList 遍历获取 ElevatorDetailHoverVO 信息, 保留2位小数
		hoverList.forEach(o -> {
			List<ElevatorDetailHoverVO> elevatorDetailHovers = o.getElevatorDetailHovers();
			elevatorDetailHovers.forEach(e -> e.setMonthCostDesc(Convert.toBigDecimal(e.getMonthCostDesc()).setScale(2, BigDecimal.ROUND_HALF_UP).toString()));
		});
		return hoverList;
	}

	@Override
	public List<ElevatorDetail> getElevatorDetailList() {
		return baseMapper.getElevatorDetailList();
	}

	@Override
	public ContractElevatorStatesVo getContractElevatorStates() {

		//合同，-1表示无状态 0:非质保，1:质保合同
		Integer[] isQuaGuarantees = {-1, 0, 1};

		ContractElevatorStatesVo contractElevatorStatesVo = new ContractElevatorStatesVo();
		List<MaintainPlanConfig> list = baseMapper.getMaintainPlanConfigList();

		//处理在保
		List<MaintainPlanConfig> zbList = list.stream().filter(o -> o.getMaintainStatus() == ElevatorMaintainStatusEnums.CONTRACT_IN_TIME.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.CONTRACT_OUT_TIME.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.DIS_ENABLE.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.OUT_TIME_STOP.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.OUT_TIME_END.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.OUT_TIME_END_HAS_NEXT.getId()).collect(Collectors.toList());
		contractElevatorStatesVo.setElevatorCountsZB(zbList.size());
		contractElevatorStatesVo.setBuildingCountsZB(zbList.stream().map(MaintainPlanConfig::getBuildingId).distinct().collect(Collectors.toList()).size());

		//处理脱保
		List<MaintainPlanConfig> tbList = list.stream().filter(o -> o.getMaintainStatus() == ElevatorMaintainStatusEnums.RELEASE_INSURANCE.getId()).collect(Collectors.toList());
		contractElevatorStatesVo.setElevatorCountsTB(tbList.size());
		contractElevatorStatesVo.setBuildingCountsTB(tbList.stream().map(MaintainPlanConfig::getBuildingId).distinct().collect(Collectors.toList()).size());

		//处理未到期质保处理
		List<MaintainPlanConfig> wzbList = list.stream().filter(o -> o.getIsQuaGuarantee().equals(isQuaGuarantees[2]) && (o.getMaintainStatus() == ElevatorMaintainStatusEnums.CONTRACT_IN_TIME.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.DIS_ENABLE.getId()) && o.getEndTime().getTime() > System.currentTimeMillis() && Convert.toInt(DateUtil.betweenDay(o.getEndTime(), new Date(), true)) > 90).collect(Collectors.toList());
		contractElevatorStatesVo.setElevatorCountsWZB(wzbList.size());
		contractElevatorStatesVo.setBuildingCountsWZB(wzbList.stream().map(MaintainPlanConfig::getBuildingId).distinct().collect(Collectors.toList()).size());

		//处理未到期非质保处理
		List<MaintainPlanConfig> wfzbList = list.stream().filter(o -> o.getIsQuaGuarantee().equals(isQuaGuarantees[1]) && (o.getMaintainStatus() == ElevatorMaintainStatusEnums.CONTRACT_IN_TIME.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.DIS_ENABLE.getId()) && o.getEndTime().getTime() > System.currentTimeMillis() && Convert.toInt(DateUtil.betweenDay(o.getEndTime(), new Date(), true)) > 90).collect(Collectors.toList());
		contractElevatorStatesVo.setElevatorCountsWFZB(wfzbList.size());
		contractElevatorStatesVo.setBuildingCountsWFZB(wfzbList.stream().map(MaintainPlanConfig::getBuildingId).distinct().collect(Collectors.toList()).size());


		//处理即将到期质保处理
		List<MaintainPlanConfig> jzbList = list.stream().filter(o -> o.getIsQuaGuarantee().equals(isQuaGuarantees[2]) && (o.getMaintainStatus() == ElevatorMaintainStatusEnums.CONTRACT_IN_TIME.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.DIS_ENABLE.getId()) && o.getEndTime().getTime() > System.currentTimeMillis() && Convert.toInt(DateUtil.betweenDay(o.getEndTime(), new Date(), true)) < 90).collect(Collectors.toList());
		contractElevatorStatesVo.setElevatorCountsJZB(jzbList.size());
		contractElevatorStatesVo.setBuildingCountsJZB(jzbList.stream().map(MaintainPlanConfig::getBuildingId).distinct().collect(Collectors.toList()).size());

		//处理即将期非质保处理
		List<MaintainPlanConfig> jfzbList = list.stream().filter(o -> o.getIsQuaGuarantee().equals(isQuaGuarantees[1]) && (o.getMaintainStatus() == ElevatorMaintainStatusEnums.CONTRACT_IN_TIME.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.DIS_ENABLE.getId()) && o.getEndTime().getTime() > System.currentTimeMillis() && Convert.toInt(DateUtil.betweenDay(o.getEndTime(), new Date(), true)) < 90).collect(Collectors.toList());
		contractElevatorStatesVo.setElevatorCountsJFZB(jfzbList.size());
		contractElevatorStatesVo.setBuildingCountsJFZB(jfzbList.stream().map(MaintainPlanConfig::getBuildingId).distinct().collect(Collectors.toList()).size());


		//处理到期质保处理
		List<MaintainPlanConfig> yzbList = list.stream().filter(o -> o.getIsQuaGuarantee().equals(isQuaGuarantees[2]) && (o.getMaintainStatus() == ElevatorMaintainStatusEnums.CONTRACT_OUT_TIME.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.OUT_TIME_STOP.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.OUT_TIME_END.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.OUT_TIME_END_HAS_NEXT.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.DIS_ENABLE.getId()) && o.getEndTime().getTime() < System.currentTimeMillis()).collect(Collectors.toList());
		contractElevatorStatesVo.setElevatorCountsYZB(yzbList.size());
		contractElevatorStatesVo.setBuildingCountsYZB(yzbList.stream().map(MaintainPlanConfig::getBuildingId).distinct().collect(Collectors.toList()).size());

		//处理到期质保处理
		List<MaintainPlanConfig> yfzbList = list.stream().filter(o -> o.getIsQuaGuarantee().equals(isQuaGuarantees[1]) && (o.getMaintainStatus() == ElevatorMaintainStatusEnums.CONTRACT_OUT_TIME.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.OUT_TIME_STOP.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.OUT_TIME_END.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.OUT_TIME_END_HAS_NEXT.getId() || o.getMaintainStatus() == ElevatorMaintainStatusEnums.DIS_ENABLE.getId()) && o.getEndTime().getTime() < System.currentTimeMillis()).collect(Collectors.toList());
		contractElevatorStatesVo.setElevatorCountsYFZB(yfzbList.size());
		contractElevatorStatesVo.setBuildingCountsYFZB(yfzbList.stream().map(MaintainPlanConfig::getBuildingId).distinct().collect(Collectors.toList()).size());

		return contractElevatorStatesVo;
	}

	@Override
	public List<ContractStatisticPlusVo> getContractStatistic_v2(Integer type) {
		List<ContractStatisticPlusVo> list = new ArrayList<>(16);
		List<ContractElevatorVo> contractStatistic_v2 = baseMapper.getContractStatistic_v2();
		//在保
		List<ContractElevatorVo> zbList = contractStatistic_v2.stream().filter(o -> o.getMaintainStatus() == 2 || o.getMaintainStatus() == 3 || o.getMaintainStatus() == 4).collect(Collectors.toList());
		//脱保
		List<ContractElevatorVo> tbList = contractStatistic_v2.stream().filter(o -> o.getMaintainStatus() == 1).collect(Collectors.toList());

		//第一步处理-在保合同电梯数
		BigDecimal sum = getContraPrices(zbList);
		List<Long> contractCounts = zbList.stream().map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts = zbList.stream().map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts = zbList.stream().map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo = new ContractStatisticPlusVo();
		contractStatisticPlusVo.setContractDesc("在保电梯合同数");
		contractStatisticPlusVo.setContractType(1);
		contractStatisticPlusVo.setContractCounts(contractCounts.size());
		contractStatisticPlusVo.setEleCounts(eleCounts.size());
		contractStatisticPlusVo.setBulCounts(bulCounts.size());
		contractStatisticPlusVo.setContractPrices(sum);


		//第二步处理-已排期电梯待续签的合同
		List<ContractElevatorVo> disContracts = baseMapper.hasPlanContract();
		BigDecimal sum2 = getContraPrices(disContracts);
		List<Long> contractCounts2 = disContracts.stream().map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts2 = disContracts.stream().map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts2 = disContracts.stream().map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo2 = new ContractStatisticPlusVo();
		contractStatisticPlusVo2.setContractDesc("已排期电梯待续签的合同数");
		contractStatisticPlusVo2.setContractType(2);
		contractStatisticPlusVo2.setContractCounts(contractCounts2.size());
		contractStatisticPlusVo2.setEleCounts(eleCounts2.size());
		contractStatisticPlusVo2.setBulCounts(bulCounts2.size());
		contractStatisticPlusVo2.setContractPrices(sum2);

		//第三步处理-在保电梯即将过期
		BigDecimal sum3 = getContraPrices(zbList.stream().filter(o -> o.getContractExtStatus() == 3).collect(Collectors.toList()));
		List<Long> contractCounts3 = zbList.stream().filter(o -> o.getContractExtStatus() == 3).map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts3 = zbList.stream().filter(o -> o.getContractExtStatus() == 3).map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts3 = zbList.stream().filter(o -> o.getContractExtStatus() == 3).map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo3 = new ContractStatisticPlusVo();
		contractStatisticPlusVo3.setContractDesc("在保电梯即将过期");
		contractStatisticPlusVo3.setContractType(3);
		contractStatisticPlusVo3.setContractCounts(contractCounts3.size());
		contractStatisticPlusVo3.setEleCounts(eleCounts3.size());
		contractStatisticPlusVo3.setBulCounts(bulCounts3.size());
		contractStatisticPlusVo3.setContractPrices(sum3);


		//第四步处理-在保电梯已到期
		BigDecimal sum4 = getContraPrices(zbList.stream().filter(o -> o.getContractStatus() == 3 || o.getContractStatus() == 4).collect(Collectors.toList()));
		List<Long> contractCounts4 = zbList.stream().filter(o -> o.getContractStatus() == 3 || o.getContractStatus() == 4).map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts4 = zbList.stream().filter(o -> o.getContractStatus() == 3 || o.getContractStatus() == 4).map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts4 = zbList.stream().filter(o -> o.getContractStatus() == 3 || o.getContractStatus() == 4).map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo4 = new ContractStatisticPlusVo();
		contractStatisticPlusVo4.setContractDesc("在保电梯已到期");
		contractStatisticPlusVo4.setContractType(4);
		contractStatisticPlusVo4.setContractCounts(contractCounts4.size());
		contractStatisticPlusVo4.setEleCounts(eleCounts4.size());
		contractStatisticPlusVo4.setBulCounts(bulCounts4.size());
		contractStatisticPlusVo4.setContractPrices(sum4);


		//第五步处理-在保电梯质保合同数
		BigDecimal sum5 = getContraPrices(zbList.stream().filter(o -> o.getIsQuaGuarantee() == 1).collect(Collectors.toList()));
		List<Long> contractCounts5 = zbList.stream().filter(o -> o.getIsQuaGuarantee() == 1).map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts5 = zbList.stream().filter(o -> o.getIsQuaGuarantee() == 1).map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts5 = zbList.stream().filter(o -> o.getIsQuaGuarantee() == 1).map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo5 = new ContractStatisticPlusVo();
		contractStatisticPlusVo5.setContractDesc("在保电梯质保合同数");
		contractStatisticPlusVo5.setContractType(5);
		contractStatisticPlusVo5.setContractCounts(contractCounts5.size());
		contractStatisticPlusVo5.setEleCounts(eleCounts5.size());
		contractStatisticPlusVo5.setBulCounts(bulCounts5.size());
		contractStatisticPlusVo5.setContractPrices(sum5);


		//第六步处理-在保电梯免保合同数
		BigDecimal sum6 = getContraPrices(zbList.stream().filter(o -> o.getMaintainTypeId() == 6).collect(Collectors.toList()));
		List<Long> contractCounts6 = zbList.stream().filter(o -> o.getMaintainTypeId() == 6).map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts6 = zbList.stream().filter(o -> o.getMaintainTypeId() == 6).map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts6 = zbList.stream().filter(o -> o.getMaintainTypeId() == 6).map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo6 = new ContractStatisticPlusVo();
		contractStatisticPlusVo6.setContractDesc("在保电梯免保合同数");
		contractStatisticPlusVo6.setContractType(6);
		contractStatisticPlusVo6.setContractCounts(contractCounts6.size());
		contractStatisticPlusVo6.setEleCounts(eleCounts6.size());
		contractStatisticPlusVo6.setBulCounts(bulCounts6.size());
		contractStatisticPlusVo6.setContractPrices(sum6);


		//第七步处理-在保电梯别墅类型合同数
		BigDecimal sum7 = getContraPrices(zbList.stream().filter(o -> o.getBulType() == 2).collect(Collectors.toList()));
		List<Long> contractCounts7 = zbList.stream().filter(o -> o.getBulType() == 2).map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts7 = zbList.stream().filter(o -> o.getBulType() == 2).map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts7 = zbList.stream().filter(o -> o.getBulType() == 2).map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo7 = new ContractStatisticPlusVo();
		contractStatisticPlusVo7.setContractDesc("在保电梯别墅类型合同数");
		contractStatisticPlusVo7.setContractType(7);
		contractStatisticPlusVo7.setContractCounts(contractCounts7.size());
		contractStatisticPlusVo7.setEleCounts(eleCounts7.size());
		contractStatisticPlusVo7.setBulCounts(bulCounts7.size());
		contractStatisticPlusVo7.setContractPrices(sum7);


		//第八步处理-在保电梯非年审合同数
		BigDecimal sum8 = getContraPrices(zbList.stream().filter(o -> o.getAuditType() == 1).collect(Collectors.toList()));
		List<Long> contractCounts8 = zbList.stream().filter(o -> o.getAuditType() == 1).map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts8 = zbList.stream().filter(o -> o.getAuditType() == 1).map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts8 = zbList.stream().filter(o -> o.getAuditType() == 1).map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo8 = new ContractStatisticPlusVo();
		contractStatisticPlusVo8.setContractDesc("在保电梯非年审合同数");
		contractStatisticPlusVo8.setContractType(8);
		contractStatisticPlusVo8.setContractCounts(contractCounts8.size());
		contractStatisticPlusVo8.setEleCounts(eleCounts8.size());
		contractStatisticPlusVo8.setBulCounts(bulCounts8.size());
		contractStatisticPlusVo8.setContractPrices(sum8);


		//第9步处理-在保电梯年审合同数
		BigDecimal sum9 = getContraPrices(zbList.stream().filter(o -> o.getAuditType() == 0).collect(Collectors.toList()));
		List<Long> contractCounts9 = zbList.stream().filter(o -> o.getAuditType() == 0).map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts9 = zbList.stream().filter(o -> o.getAuditType() == 0).map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts9 = zbList.stream().filter(o -> o.getAuditType() == 0).map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo9 = new ContractStatisticPlusVo();
		contractStatisticPlusVo9.setContractDesc("在保电梯年审合同数");
		contractStatisticPlusVo9.setContractType(9);
		contractStatisticPlusVo9.setContractCounts(contractCounts9.size());
		contractStatisticPlusVo9.setEleCounts(eleCounts9.size());
		contractStatisticPlusVo9.setBulCounts(bulCounts9.size());
		contractStatisticPlusVo9.setContractPrices(sum9);


		//第10步处理-拖保合同电梯数
		BigDecimal sum10 = getContraPrices(tbList);
		List<Long> contractCounts10 = tbList.stream().map(ContractElevatorVo::getId).distinct().collect(Collectors.toList());
		List<Long> eleCounts10 = tbList.stream().map(ContractElevatorVo::getElevatorId).distinct().collect(Collectors.toList());
		List<Long> bulCounts10 = tbList.stream().map(ContractElevatorVo::getBuildingId).distinct().collect(Collectors.toList());
		ContractStatisticPlusVo contractStatisticPlusVo10 = new ContractStatisticPlusVo();
		contractStatisticPlusVo10.setContractDesc("拖保电梯合同数");
		contractStatisticPlusVo10.setContractType(10);
		contractStatisticPlusVo10.setContractCounts(contractCounts10.size());
		contractStatisticPlusVo10.setEleCounts(eleCounts10.size());
		contractStatisticPlusVo10.setBulCounts(bulCounts10.size());
		contractStatisticPlusVo10.setContractPrices(sum10);

		list.add(contractStatisticPlusVo);
		list.add(contractStatisticPlusVo2);
		list.add(contractStatisticPlusVo3);
		list.add(contractStatisticPlusVo4);
		list.add(contractStatisticPlusVo5);
		list.add(contractStatisticPlusVo6);
		list.add(contractStatisticPlusVo7);
		list.add(contractStatisticPlusVo8);
		list.add(contractStatisticPlusVo9);
		list.add(contractStatisticPlusVo10);
		return list;
	}

	@Override
	public List<ConEleDisposableVo> downExcel() {
		//获取当前合同信息
		List<ConEleDisposableVo> list = baseMapper.downExcel();
		List<BuildingContractPlus> list1 = this.list();
		Map<Long, List<BuildingContractPlus>> longListMap = list1.stream().filter(o -> o.getParentId() != null).collect(Collectors.groupingBy(BuildingContractPlus::getParentId));
		for (ConEleDisposableVo conEleDisposableVo : list) {
			if (conEleDisposableVo.getNowCreateTime() != null) {
				MaintainPlanContent maintainPlanContent = maintainPlanContentService.getOne(Wrappers.<MaintainPlanContent>lambdaQuery().eq(MaintainPlanContent::getTypeStatus, 1).eq(MaintainPlanContent::getElevatorId, conEleDisposableVo.getElevatorId()).between(MaintainPlanContent::getPlanDate, conEleDisposableVo.getNowContractBeginTime(), conEleDisposableVo.getNowContractEndTime()).last("limit 1"));
				conEleDisposableVo.setNowIsPlan(maintainPlanContent != null);
				BuildingContractPlus buildingContractPlus = baseMapper.getDetailByElevator_v2(conEleDisposableVo.getElevatorId(), conEleDisposableVo.getNowContractBeginTime());
				if (buildingContractPlus != null) {
					conEleDisposableVo.setLastContractId(buildingContractPlus.getId());
					conEleDisposableVo.setLastStatus(buildingContractPlus.getStatus());
					conEleDisposableVo.setLastCreateTime(buildingContractPlus.getCreateTime());
					conEleDisposableVo.setLastContractBeginTime(buildingContractPlus.getContractBeginTime());
					conEleDisposableVo.setLastContractEndTime(buildingContractPlus.getContractEndTime());
					conEleDisposableVo.setLastContractEndTime(buildingContractPlus.getContractEndTime());
					MaintainPlanContent maintainPlanContent2 = maintainPlanContentService.getOne(Wrappers.<MaintainPlanContent>lambdaQuery().eq(MaintainPlanContent::getTypeStatus, 1).eq(MaintainPlanContent::getElevatorId, conEleDisposableVo.getElevatorId()).between(MaintainPlanContent::getPlanDate, buildingContractPlus.getContractBeginTime(), buildingContractPlus.getContractEndTime()).last("limit 1"));
					conEleDisposableVo.setLastIsPlan(maintainPlanContent2 != null);
				}
			}

			List<BuildingContractPlus> plus = longListMap.get(conEleDisposableVo.getNowContractId());
			if (plus != null && plus.size() > 0) {
				BuildingContractPlus buildingContractPlus = plus.get(0);
				conEleDisposableVo.setNextContractId(buildingContractPlus.getId());
				conEleDisposableVo.setNextStatus(buildingContractPlus.getStatus());
				conEleDisposableVo.setNextCreateTime(buildingContractPlus.getCreateTime());
				conEleDisposableVo.setNextContractBeginTime(buildingContractPlus.getContractBeginTime());
				conEleDisposableVo.setNextContractEndTime(buildingContractPlus.getContractEndTime());
				conEleDisposableVo.setNextContractEndTime(buildingContractPlus.getContractEndTime());
				MaintainPlanContent maintainPlanContent2 = maintainPlanContentService.getOne(Wrappers.<MaintainPlanContent>lambdaQuery().eq(MaintainPlanContent::getTypeStatus, 1).eq(MaintainPlanContent::getElevatorId, conEleDisposableVo.getElevatorId()).between(MaintainPlanContent::getPlanDate, buildingContractPlus.getContractBeginTime(), buildingContractPlus.getContractEndTime()).last("limit 1"));
				conEleDisposableVo.setNextIsPlan(maintainPlanContent2 != null);
			}

		}
		return list;
	}

	@Override
	public BuildingContractPlus getNext(Long elevatorId) {
		return baseMapper.getNext(elevatorId);
	}

	@Override
	public List<BuildingContractPlus> getUpdateEndTimeList() {
		ContractSearchDTO contractSearchDTO = new ContractSearchDTO();
		contractSearchDTO.setType(0);
		List<BuildingContractPlus> list = baseMapper.getContractStatistic(contractSearchDTO);
		return list;
	}

	@Override
	public ContractStatusCountVo getContractStatistic(ContractSearchDTO contractSearchDTO) {
		ContractStatusCountVo contractStatusCountVo = new ContractStatusCountVo();
		//第一步处理 服务电梯合同--待续签合同
		if (contractSearchDTO.getType() == 1) {
			List<BuildingContractPlus> list = baseMapper.getContractStatistic(contractSearchDTO);
			Integer sixtyDayCounts = 0;
			Integer ninetyDayCounts = 0;
			Integer timeOutCounts = 0;
			for (BuildingContractPlus buildingContractPlus : list) {
				if (buildingContractPlus.getMaintainStatus().equals(ElevatorMaintainStatusEnums.CONTRACT_IN_TIME.getId()) && buildingContractPlus.getContractStatus() == 2) {
					Integer endDay = Convert.toInt(buildingContractPlus.getAboutEndDay(), 0);
					if (endDay < 90) {
						ninetyDayCounts = ninetyDayCounts + 1;
					}
					if (endDay < 60) {
						sixtyDayCounts = sixtyDayCounts + 1;
					}
				}
				if (buildingContractPlus.getMaintainStatus().equals(ElevatorMaintainStatusEnums.OUT_TIME_STOP.getId()) || buildingContractPlus.getMaintainStatus().equals(ElevatorMaintainStatusEnums.OUT_TIME_END.getId()) || buildingContractPlus.getMaintainStatus().equals(ElevatorMaintainStatusEnums.OUT_TIME_END_HAS_NEXT.getId())) {
					timeOutCounts = timeOutCounts + 1;
				}
			}
			contractStatusCountVo.setSixtyDayCounts(sixtyDayCounts);
			contractStatusCountVo.setNinetyDayCounts(ninetyDayCounts);
			contractStatusCountVo.setTimeOutCounts(timeOutCounts);
			contractStatusCountVo.setDisCount(ninetyDayCounts + timeOutCounts);

		}

		//第一步处理 非服务电梯合同--待续签合同
		if (contractSearchDTO.getType() == 0) {
			List<BuildingContractPlus> list = baseMapper.getContractStatistic(contractSearchDTO);
			Integer sixtyDayCounts = 0;
			Integer ninetyDayCounts = 0;
			Integer timeOutCounts = 0;
			for (BuildingContractPlus buildingContractPlus : list) {
				if (buildingContractPlus.getContractStatus().equals(CONTRACT_STATUS[2])) {
					Integer endDay = Convert.toInt(buildingContractPlus.getAboutEndDay(), 0);
					if (endDay < 90) {
						ninetyDayCounts = ninetyDayCounts + 1;
					}
					if (endDay < 60) {
						sixtyDayCounts = sixtyDayCounts + 1;
					}
				}
				if (buildingContractPlus.getContractStatus().equals(CONTRACT_STATUS[3]) || buildingContractPlus.getContractStatus().equals(CONTRACT_STATUS[4])) {
					timeOutCounts = timeOutCounts + 1;
				}
			}
			contractStatusCountVo.setSixtyDayCounts(sixtyDayCounts);
			contractStatusCountVo.setNinetyDayCounts(ninetyDayCounts);
			contractStatusCountVo.setTimeOutCounts(timeOutCounts);
			contractStatusCountVo.setDisCount(sixtyDayCounts + ninetyDayCounts + timeOutCounts);
		}

		//设置待审核数
		ContractSearchDTO contractSearchDTO2 = new ContractSearchDTO();
		contractStatusCountVo.setDisCheckCounts(Convert.toInt(getContractDisCheckList(contractSearchDTO2).getTotal()));

		//设置服务电梯数
		IPage<ContractInTimeVo> inTimeList = getContractInTimeList(contractSearchDTO2);
		contractStatusCountVo.setInTimeCounts(Convert.toInt(inTimeList.getTotal(), 0));

		//设置脱保数
		contractStatusCountVo.setTuoCounts(Convert.toInt(getContractTuoList(contractSearchDTO2).getTotal()));
		return contractStatusCountVo;
	}

	@Override
	public IPage<ContractListVo> getContractStatisticList(ContractSearchDTO contractSearchDTO) {
		Query query = new Query();
		query.setSize(contractSearchDTO.getSize());
		query.setCurrent(contractSearchDTO.getCurrent());
		IPage<ContractListVo> page = Condition.getPage(query);
		if (contractSearchDTO.getTowSearchType() == 3) {
			contractSearchDTO.setContractStatus(3);
		} else {
			contractSearchDTO.setContractStatus(2);
		}
		if (contractSearchDTO.getType() == 1) {
			List<ContractListVo> aboutEndTimeList = baseMapper.getAboutEndTimeList(query.getSize() == 0 ? null : page, contractSearchDTO);
			page.setRecords(aboutEndTimeList);
		} else {
			List<ContractListVo> aboutEndTimeList = baseMapper.getDisAboutEndTimeList(query.getSize() == 0 ? null : page, contractSearchDTO);
			page.setRecords(aboutEndTimeList);
		}
		for (ContractListVo contractListVo : page.getRecords()) {
			if (contractListVo.getEndDay() != -1) {
				if (contractListVo.getEndDay() <= 2) {
					contractListVo.setEndDayDesc("刚刚到期");
				} else if (contractListVo.getEndDay() > 2 && contractListVo.getEndDay() < 6) {
					contractListVo.setEndDayDesc("超3天");
				} else if (contractListVo.getEndDay() >= 6 && contractListVo.getEndDay() < 30) {
					contractListVo.setEndDayDesc("超7天");
				} else if (contractListVo.getEndDay() >= 30 && contractListVo.getEndDay() < 60) {
					contractListVo.setEndDayDesc("超1个月");
				} else if (contractListVo.getEndDay() >= 60) {
					contractListVo.setEndDayDesc("超2个月");
				}
			} else {
				contractListVo.setEndDayDesc("-");
			}
			if (contractListVo.getAboutEndDay() != -1) {
				if (contractListVo.getAboutEndDay() < 90) {
					contractListVo.setAboutEndDayDesc("不足90天");
				}
				if (contractListVo.getAboutEndDay() < 60) {
					contractListVo.setAboutEndDayDesc("不足60天");
				}
				if (contractListVo.getAboutEndDay() < 30) {
					contractListVo.setAboutEndDayDesc("不足30天");
				}
				if (contractListVo.getAboutEndDay() < 7) {
					contractListVo.setAboutEndDayDesc("不足7天");
				}
			} else {
				contractListVo.setAboutEndDayDesc("-");
			}

		}
		return page;
	}


	@Override
	public IPage<ContractListVo> getContractDisCheckList(ContractSearchDTO contractSearchDTO) {
		Query query = new Query();
		query.setSize(contractSearchDTO.getSize());
		query.setCurrent(contractSearchDTO.getCurrent());
		IPage<ContractListVo> page = Condition.getPage(query);
		List<ContractListVo> list = baseMapper.getContractDisCheckList(query.getSize() == 0 ? null : page, contractSearchDTO);
		for (ContractListVo contractListVo : list) {
			if (contractListVo.getDisCheckDay() == 0) {
				contractListVo.setDisCheckDayDesc("不足1天");
			} else if (contractListVo.getDisCheckDay() == 1) {
				contractListVo.setDisCheckDayDesc("已等待1天");
			} else if (contractListVo.getDisCheckDay() == 2) {
				contractListVo.setDisCheckDayDesc("已等待2天");
			} else if (contractListVo.getDisCheckDay() == 3) {
				contractListVo.setDisCheckDayDesc("已等待3天");
			} else if (contractListVo.getDisCheckDay() == 4) {
				contractListVo.setDisCheckDayDesc("已等待4天");
			} else if (contractListVo.getDisCheckDay() == 5) {
				contractListVo.setDisCheckDayDesc("已等待5天");
			} else if (contractListVo.getDisCheckDay() == 6) {
				contractListVo.setDisCheckDayDesc("已等待6天");
			} else if (contractListVo.getDisCheckDay() > 6) {
				contractListVo.setDisCheckDayDesc("已等待7天以上");
			}
		}

		page.setRecords(list);
		return page;
	}


	@Override
	public IPage<ContractInTimeVo> getContractInTimeList(ContractSearchDTO contractSearchDTO) {
		Query query = new Query();
		query.setSize(contractSearchDTO.getSize());
		query.setCurrent(contractSearchDTO.getCurrent());
		IPage<ContractInTimeVo> page = Condition.getPage(query);
		List<ContractInTimeVo> list = baseMapper.getContractInTimeList(query.getSize() == 0 ? null : page, contractSearchDTO);
		for (ContractInTimeVo record : list) {
			if (record.getStatus() == 1) {
				record.setAllStatusDesc("未审核");
			} else if (record.getContractStatus() == 1) {
				record.setAllStatusDesc("未生效");
			} else if (record.getContractStatus() == 2) {
				record.setAllStatusDesc("合同期内");
			} else if (record.getContractStatus() == 3) {
				record.setAllStatusDesc("合同到期");
			} else if (record.getContractStatus() == 4) {
				record.setAllStatusDesc("已终止");
			} else if (record.getContractExtStatus() == 5) {
				record.setAllStatusDesc("已作废");
			} else if (record.getStatus() == 3) {
				record.setAllStatusDesc("审核不通过");
			} else {
				record.setAllStatusDesc("-");
			}
		}

		page.setRecords(list);
		return page;
	}


	@Override
	public IPage<ContractListVo> getContractTuoList(ContractSearchDTO contractSearchDTO) {
		Query query = new Query();
		query.setSize(contractSearchDTO.getSize());
		query.setCurrent(contractSearchDTO.getCurrent());
		IPage<ContractListVo> page = Condition.getPage(query);
		List<ContractListVo> list = baseMapper.getContractTuoList(query.getSize() == 0 ? null : page, contractSearchDTO);
		page.setRecords(list);
		return page;
	}


	/**
	 * 2024-03-11 合同信息需求调整，新增服务内容等信息
	 *
	 * @param buildingContractPlus
	 */
	private void saveContractExtV2(BuildingContractPlus buildingContractPlus) {

		//第一步判断内容是否为空，
		BuildingContractExtPlus buildingContractExtPlus = buildingContractPlus.getBuildingContractExtPlus();
		if (buildingContractExtPlus == null) {
			return;
		}
		//第一步不为空,更新内容
		BuildingContractExtPlus contractExtPlus = buildingContractExtPlusService.getOne(Wrappers.<BuildingContractExtPlus>lambdaQuery().eq(BuildingContractExtPlus::getContractId, buildingContractPlus.getId()));
		if (contractExtPlus == null) {
			//表示新增未同步
			Integer status = 2;
			buildingContractExtPlus.setStatus(status);
			buildingContractExtPlus.setContractId(buildingContractPlus.getId());
			buildingContractExtPlusService.save(buildingContractExtPlus);
		} else {
			buildingContractExtPlus.setId(contractExtPlus.getId());
			buildingContractExtPlusService.updateById(buildingContractExtPlus);
		}

		//第三步合同保养时间
		List<MaintainTimeSubmitDTO> buildingMaintainTimes = buildingContractExtPlus.getBuildingMaintainTimes();
		if (buildingMaintainTimes == null) {
			return;
		}

		if (!buildingContractExtPlus.getAllowMaintain().equals(0)) {
			buildingContractMaintainTimeService.submit(buildingContractPlus.getId(), buildingContractExtPlus.getBuildingMaintainTimes());
		}

	}


	/**
	 * 更新电梯的第一份的合同状态
	 * 合同来源
	 * 1:新签
	 * 2:续签
	 * 3:转签
	 * 4:丢失
	 * 5:夺回
	 * 6:新梯移交
	 * 7:免保转签
	 *
	 * @param elevatorId
	 * @param anInt
	 */
	private void updateElevator(Long elevatorId, Integer anInt, Integer contractPassStatus) {
		Elevator elevator = elevatorService.getById(elevatorId);
		if (Convert.toInt(elevator.getContractPassStatus(), -1) != 1) {
			//审核通过以审核通过为主
			if (contractPassStatus != null) {
				elevator.setContractPassStatus(contractPassStatus);
				elevator.setFirstContractStatus(5);
				if (anInt == 1 || anInt == 2) {
					elevator.setFirstContractStatus(2);
				} else if (anInt == 5) {
					elevator.setFirstContractStatus(3);
				} else if (anInt == 6) {
					elevator.setFirstContractStatus(4);
				}
			} else {
				//没有审核通过以第一份创建为主
				if (elevator.getFirstContractStatus() == null || elevator.getFirstContractStatus() == 1) {
					elevator.setFirstContractStatus(5);
					if (anInt == 1 || anInt == 2) {
						elevator.setFirstContractStatus(2);
					} else if (anInt == 5) {
						elevator.setFirstContractStatus(3);
					} else if (anInt == 6) {
						elevator.setFirstContractStatus(4);
					}
				}
			}
			elevatorService.updateById(elevator);
		}
	}


	private BigDecimal getContraPrices(List<ContractElevatorVo> list) {
		List<ContractElevatorVo> list2 = new ArrayList<>();
		for (ContractElevatorVo contractElevatorVo : list) {
			ContractElevatorVo contractElevatorVo1 = new ContractElevatorVo();
			contractElevatorVo1.setId(contractElevatorVo.getId());
			contractElevatorVo1.setContractCost(contractElevatorVo.getContractCost());
			list2.add(contractElevatorVo1);
		}
		list = list2.stream().distinct().collect(Collectors.toList());
		BigDecimal sum = Convert.toBigDecimal(0);
		for (ContractElevatorVo contractElevatorVo : list) {
			sum = sum.add(Convert.toBigDecimal(contractElevatorVo.getContractCost(), Convert.toBigDecimal(0)));
		}
		return sum.setScale(2, RoundingMode.HALF_UP);
	}
}
